package com.cn.scenario.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.scenario.entity.Curriculum;
import com.cn.scenario.entity.CurriculumLog;
import com.cn.scenario.mapper.CurriculumLogMapper;
import com.cn.scenario.mapper.CurriculumMapper;
import com.cn.scenario.service.CurriculumService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author Boolean
 * @Date 2022/12/28 22:37
 * @Version 1.0
 */
@Service
@Slf4j
public class CurriculumServiceImpl  extends ServiceImpl<CurriculumMapper, Curriculum> implements CurriculumService {
    private static final String LOCK_KEY = "COMMERCE-BUSINESS";

    @Resource
    CurriculumLogMapper curriculumLogMapper;

    @Resource
    RedissonClient redissonClient;


    /**
     * @description: 课程选择
     * @author: 任秀兴
     * @date: 2022/12/29 17:48
     * @param: [id, name]
     * @return: java.lang.Boolean
     **/
    public Boolean CourseSelection(int id,String name){
        boolean success = false;
        Curriculum curriculum = this.getById(id);
        if(curriculum.getNum()>0){
            curriculum.setNum(curriculum.getNum()-1);
            success = this.updateById(curriculum);

            CurriculumLog curriculumLog = new CurriculumLog();
            curriculumLog.setCid(id);
            curriculumLog.setName(name);
            curriculumLog.setNum(curriculum.getTotal()-curriculum.getNum());
            curriculumLogMapper.insert(curriculumLog);
        }

        return success;
    }


    /**
     * @description: 课程选择  加入分布式锁
     * @author: 任秀兴
     * @date: 2022/12/29 17:47
     * @param: [id, name]
     * @return: java.lang.Boolean
     **/
    public Boolean CourseSelectionLock(int id,String name){
        boolean success = false;
        Curriculum curriculum = this.getById(id);
        if (curriculum.getNum() > 0) {
            RLock lock = redissonClient.getLock(LOCK_KEY);
            /*可重入锁：*/
            if (lock.tryLock()) {
                try {
                    curriculum.setNum(curriculum.getNum() - 1);
                    success = this.updateById(curriculum);

                    CurriculumLog curriculumLog = new CurriculumLog();
                    curriculumLog.setCid(id);
                    curriculumLog.setName(name);
                    curriculumLog.setNum(curriculum.getTotal() - curriculum.getNum());
                    curriculumLogMapper.insert(curriculumLog);
                } catch (Exception e) {
                    e.printStackTrace();

                } finally {
                    lock.unlock();
                    System.out.println("Lock Released");
                }
            } else {
                System.out.println("Can not get lock");
            }
        }
        return success;
    }











}
